package com.enjoy.mjbaseframe.base;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.enjoy.mjbaseframe.okgo.OkGoLoader;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * fragment基础类
 * Created by YoKeyword on 16/6/5.
 */
public abstract class BaseFragment extends SupportFragment {
    protected BaseActivity mActivity;
    public Unbinder mUnbinder = null;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = (BaseActivity) activity;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = null;
        if (setLayout() instanceof Integer) {
            rootView=inflater.inflate((Integer) setLayout(),container,false);
        }else if(setLayout() instanceof View){
            rootView= (View) setLayout();
        }else{
            throw new ClassCastException("type of setLayout() must be int or View!");
        }
        mUnbinder = ButterKnife.bind(this,rootView);
        init();
        return rootView;
    }

    @Override
    public void onDestroy() {
        if (mUnbinder!=null) {
            mUnbinder.unbind();
            mUnbinder=null;
        }
        OkGoLoader.getInstance().clearTags();
        super.onDestroy();
    }

    public abstract Object setLayout();
    public void init(){}
}
